#!/usr/bin/python

"""Tests for resource.py
"""

__author__ = 'elsigh@google.com (Lindsey Simon)'


import datetime
import logging
import os
import random
import re
import time
import urllib
import urllib2
import unittest

import resource


HOST = 'http://localhost:8086/'
HOST = 'http://resource-cgi.appspot.com/'

EXPIRES_FORMAT = '%a, %d %b %Y %H:%M:%S %Z'


class ResourceUnitTest(unittest.TestCase):

  def testExpiresPast(self):
    f = urllib2.urlopen('%s%s' % (HOST, '?expires=-1'))
    headers = f.info()
    datestring = headers['Expires']
    expires = time.strptime(datestring, EXPIRES_FORMAT)
    now = time.localtime()
    self.assertTrue(now > expires)


  def testExpiresFuture(self):
    f = urllib2.urlopen('%s%s' % (HOST, '?expires=1'))
    headers = f.info()
    datestring = headers['Expires']
    expires = time.strptime(datestring, EXPIRES_FORMAT)
    now = time.localtime()
    self.assertTrue(now < expires)


  def testExpiresNone(self):
    # 0 = no expires
    f = urllib2.urlopen('%s%s' % (HOST, '?expires=0'))
    headers = f.info()
    self.assertRaises(KeyError, lambda: headers['Expires'])


  def testContentTypeHeaders(self):
    for content_type in resource.TYPE_HEADERS.keys():
      f = urllib2.urlopen('%s%s' % (HOST, '?type=%s' % content_type))
      headers = f.info()
      content_type_header = headers['Content-Type']
      search = re.search(resource.TYPE_HEADERS[content_type],
                         content_type_header)
      self.assertTrue(search != None, '%s does not match %s' %
          (resource.TYPE_HEADERS[content_type], content_type_header))


  def testLastModifiedPast(self):
    f = urllib2.urlopen('%s%s' % (HOST, '?last=-1'))
    headers = f.info()
    datestring = headers['Last-Modified']
    last_modified = time.strptime(datestring, EXPIRES_FORMAT)
    now = time.localtime()
    self.assertTrue(last_modified < now)


  def testLastModifiedNone(self):
    f = urllib2.urlopen('%s%s' % (HOST, '?last=0'))
    headers = f.info()
    self.assertRaises(KeyError, lambda: headers['Last-Modified'])


  def testRedirect(self):
    url = '%s%s' % (HOST, '?redir=1')
    f = urllib2.urlopen(url)
    headers = f.info()
    self.assertEquals(HOST, f.geturl())


  def testRedirectWithUrl(self):
    redirurl = '%s%s' % (HOST, '?gotredirected=1')
    url = '%s%s' % (HOST, '?redir=1&redirurl=%s' %
                          urllib.quote(redirurl))
    f = urllib2.urlopen(url)
    headers = f.info()
    self.assertEquals(redirurl, f.geturl())


  def testCookieParam(self):
    cookie_length = 5
    url = '%s%s' % (HOST, '?cookie=%s' % cookie_length)
    f = urllib2.urlopen(url)
    headers = f.info()
    cookie = headers['Set-Cookie']
    self.assertEquals(cookie, resource.GetCookieText(cookie_length))


  def testSleep(self):
    sleep = 2
    url = '%s%s' % (HOST, '?sleep=%s' % sleep)
    now1 = time.localtime()
    f = urllib2.urlopen(url)
    now2 = time.localtime()
    self.assertTrue(now2[5] - now1[5] >= sleep,
        'now2(%s) - now1(%s) should be >= %s' % (now2[5], now1[5], sleep))


  def testAcceptEncodingHeader(self):
    url = '%s%s' % (HOST, '?type=jsgz')
    req = urllib2.Request(url)
    req.add_header('HTTP_USER_AGENT', 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.3 (KHTML, like Gecko) Chrome/5.0.360.0 Safari/533.3')
    req.add_header('ACCEPT_ENCODING', 'gzip')
    f = urllib2.urlopen(req)
    headers = f.info()
    print ('headers: %s' % headers)
    content = f.read()
    print ('len: %s' % len(content))
    #print ('content: %s' % content)
    self.assertTrue(False)


if __name__ == '__main__':
  unittest.main()
